class ScenesMgmt {
	static self: ScenesMgmt;


    /**************************** 单例模式构建 ****************************/

    static create(parent: egret.DisplayObjectContainer) {
        !ScenesMgmt.self ? ScenesMgmt.self = new ScenesMgmt(parent) : "";
        return ScenesMgmt.self;
    }


	private parent: egret.DisplayObjectContainer;		//场景父容器
    private curScene: egret.DisplayObjectContainer;		//当前场景
	public prevScene: any;								//上一个场景


	/**************************** 初始化构建 ****************************/

    public constructor(parent: egret.DisplayObjectContainer) {
        this.parent = parent;

		//初始化资源管理器
		ResMgmt.create();
    }


	/**************************** 场景切换入口 ****************************/

    public changeScene(scene: any, callback?: Function, thisObject?: Object) {
		//载入loading资源
		ResMgmt.self.update(
			
			//loading资源配置
			{
				url: scene.loading.resUrl,
				root: scene.loading.resRoot,
				groupName: scene.loading.resGroup
			},

			//loading资源载入完成回调
			() => {

				//实例化loading，并添加至舞台
				let loading: LoadingUI = new scene.loading();
				this.parent.addChild(loading);

				//载入场景资源
				ResMgmt.self.update(

					//场景资源配置
					{
						url: scene.resUrl,
						root: scene.resRoot,
						groupName: scene.resGroup
					},

					//场景资源载入完成回调
					() => {

						//移除loading
						this.parent.removeChild(loading);

						//主场景直接执行回调
						if (scene === Main) {
							callback ? callback.call(thisObject) : "";
						}

						//子场景进行切换
						else {

							//实例化子场景，并添加至舞台，且移除上一个场景
							let newScene = new scene();
							this.prevScene = this.curScene;
							this.curScene ? Effect.slideRightOut(this.curScene, this.parent) : "";
							this.curScene = newScene;
							Effect.slideRightIn(newScene, this.parent);

							//执行完成回调
							callback ? callback(newScene) : "";
						}
					},

					//场景资源配置进度回调
					(itemsLoaded, itemsTotal) => {

						//更新场景载入进度
						loading.setProgress(itemsLoaded, itemsTotal);
					}
				);
			}
		);
	}
}